<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/StoresSystem' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/StoresSystem/ShopsList' }">查看门店</el-breadcrumb-item>
      <el-breadcrumb-item>{{nowShop.name}}</el-breadcrumb-item>
    </el-breadcrumb>

    <div class="bians">
      <el-input placeholder="请输入查询内容" v-model="search.searchValue" class="input-with-select">
        <el-select v-model="search.searchType" slot="prepend" placeholder="请选择">
          <el-option label="商品名字" value="name"></el-option>
          <el-option label="商品价格" value="price"></el-option>
          <el-option label="商品数量" value="num"></el-option>
        </el-select>
        <el-button slot="append" icon="el-icon-search" @click="serchGoods"></el-button>
      </el-input>
    </div>
    <el-table size="small" :data="pageData.rows" style="width: 100%" height="360px">
      <el-table-column label="商品编号" prop="_id" width="190%"></el-table-column>
      <el-table-column label="商品名字" prop="name"></el-table-column>
      <el-table-column label="商品价格" prop="price"></el-table-column>
      <el-table-column label="商品数量" prop="num"></el-table-column>
      <el-table-column label="商品门店" prop="shopsId.name"></el-table-column>
      <el-table-column label="商品图片">
        <template slot-scope="scope">
          <img class="studentsImage" :src="scope.row.imagePath" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="small" @click="updateGoods(scope.row)">修改</el-button>

          <el-dialog title="修改信息" :visible.sync="centerDialogVisible">
            <el-form :model="getUpdateGood">
              <el-form-item label="商品名字：">
                <el-input autocomplete="off" v-model="getUpdateGood.name"></el-input>
              </el-form-item>
              <el-form-item label="商品价格：">
                <el-input autocomplete="off" v-model="getUpdateGood.price"></el-input>
              </el-form-item>
              <el-form-item label="商品数量：">
                <el-input autocomplete="off" v-model="getUpdateGood.num"></el-input>
              </el-form-item>
              <el-form-item label="商品图片：">
                <el-upload
                  class="avatar-uploader"
                  action="/goods/uploadImages"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload">
                  <img v-if="getUpdateGood.imagePath" :src="getUpdateGood.imagePath" class="avatar">
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="centerDialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="updata(getUpdateGood)" :plain="true">确 定</el-button>
            </div>
          </el-dialog>

          <el-button size="mini" type="danger" @click="removeGoods(scope.row)" :plain="true">下架</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageData.currentPage"
        :page-sizes="[4,5,6,7]"
        :page-size="pageData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageData.total"
      ></el-pagination>
    </div>
    <el-button size="mini"  @click="showgoods()"  id="showdq">关闭当前页图形</el-button>
    <div id="myChart" :style="{width: '1000px', height: '200px'}"></div>
  </div>
</template>

<script>
import axios from "axios";
import { createNamespacedHelpers } from "vuex";
const {
  mapState,
  mapGetters,
  mapMutations,
  mapActions
} = createNamespacedHelpers("goodsList");

export default {
  data() {
    return {
      // search: {
      //   searchType: "",
      //   searchValue: ""
      // },
      centerDialogVisible: false,
      getUpdateGood: {
        name: "",
        price: "",
        num: "",
        shopsId: "",
        imagePath:""
      },
      shopsId: "",
      nowShop:{}
    };
  },
    computed: {
    ...mapState(["pageData","search"]),
    getStoreItem () {
        return this.pageData.rows
      } 
  },
  watch: {
      getStoreItem () {
        // console.log(this.pageData.rows,'asfas fas fas')
        let obj = this.pageData.rows.map( item => 
      {
        return {value:item.num,name:item.name}
      }
      )
      console.log(obj);
      this.showdata = obj
      this.drawLine(); 
      }
    },
  created() {
    this.shopsId = this.$route.params._id;
    this.getGoodsCellAsync(this.shopsId);
    this.getNowShop()
  },
  methods: {
    async getNowShop(){
      const {data} =await axios.get("/shops/getShops",{params:{_id:this.shopsId}})
      this.nowShop = data.rows[0]

    },


    ...mapMutations(["changepageSize", "changePage"]),
    // ...mapMutations(["updateStudents", "close"]),
    ...mapActions([
      "getGoodsCellAsync",
      "deleteGoodsCellAsync",
      "updataGoodsCellAsync"
    ]),
    //点击修改按钮
    updateGoods(row) {
      this.centerDialogVisible = true;
      this.getUpdateGood = { ...row };
      console.log(row);
    },

    //下架
    removeGoods(row) {
      this.deleteGoodsCellAsync(row._id);
      this.$message({
        showClose: true,
        message: "下架成功",
        type: "success"
      });

      this.getGoodsCellAsync(this.shopsId);
    },
    //分页
    handleCurrentChange(val) {
      this.changePage(val);
      this.getGoodsCellAsync(this.shopsId);
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.changepageSize(val);
      this.getGoodsCellAsync(this.shopsId);
    },
    // 点击修改改变数据
    updata(newGoodData) {
      this.updataGoodsCellAsync({ ...newGoodData });
      //  this.getGoodsCellAsync();
      this.$message({
        showClose: true,
        message: "修改成功",
        type: "success"
      });
      this.centerDialogVisible = false;
      this.getGoodsCellAsync(this.shopsId);
    },
    //
    serchGoods() {
      console.log();
      //  this.getStudentsAsync()
      this.getGoodsCellAsync(this.shopsId,this.search);
    },
    //修改图片
    handleAvatarSuccess(response){
      console.log(response);
      this.getUpdateGood.imagePath ="./temp/" + response.filename[0]
      
    },
    beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2M!');
        }
        return isJPG && isLt2M;
      },
    //图表
        showgoods(){
      var root=document.getElementById('myChart');
      var root2=document.getElementById('showdq');
      console.log(root2.innerText);
      if(root2.innerText == "图形化当前页数据"){
        root.style.cssText="height:200px";
        root2.innerText = "关闭当前页图形"
      }else{
        root.style.cssText="height:0px";
        root2.innerText = "图形化当前页数据"
      }
      
      
       
    },
      //图表
    drawLine(){
      
        // 基于准备好的dom，初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('myChart'))
        // 绘制图表
        myChart.setOption({
            title: { text: '' },
            tooltip: {},
            series : [
        {
            name: '访问来源',
            type: 'pie',
            radius: '55%',
            data:this.showdata
        }
    ]
        });
    }
    //////////////////////////biao =========
  }
};
</script>

<style>
.el-breadcrumb {
  padding-left: 10px;
  height: 30px;
}

.el-table__header-wrapper .has-gutter tr th {
  line-height: 60px;
}
.has-gutter .cell {
  text-align: center;
  font-size: 14px;
}
.el-table__row .cell {
  text-align: center;
}
.is-right button {
  margin-right: 5px;
}
.el-dialog {
  width: 500px;
}
.el-form-item {
  padding: 0 40px;
}
.el-dialog__header {
  display: flex;
}
.el-dialog .el-input {
  width: 260px;
}
.el-dialog__close {
  display: none;
}
.dialog-footer {
  padding-right: 40px;
}

.el-pagination {
  margin-top: 10px;
  height: 30px !important;
}
.studentsImage {
  width: 50px;
}
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
.bans {
  line-height: 60px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 60px;
    height: 60px;
    line-height: 60px;
    text-align: center;
  }
  .avatar {
    width: 60px;
    height: 60px;
    display: block;
  }

  
.el-table_1_column_6 .cell img{
  width: 50px !important;
  height: 50px !important;
}
</style>